package com.zksk.admin.controller.portal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zksk.admin.api.domain.portal.vo.SanctionsRequestVo;
import com.zksk.admin.service.portal.SanctionsService;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.utils.StringUtils;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.Sanctions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 奖惩信息表
 */
@Slf4j
@RestController
@RequestMapping("/portal/sanctions")
@Api(value = "SanctionsController", tags = "信息公示相关接口")
public class SanctionsController extends BaseController {

    @Autowired
    private SanctionsService sanctionsService;

    @Log(title = "奖惩信息列表", businessType = BusinessType.SELECT)
    @ApiOperation(value = "奖惩信息列表")
    @PostMapping("/list")
    public R<Page<Sanctions>> list(@RequestBody SanctionsRequestVo requestVo){
        //构建条件
        LambdaQueryWrapper<Sanctions> wrapper = new LambdaQueryWrapper<Sanctions>()
                .like(StringUtils.isNotBlank(requestVo.getTitleName()), Sanctions::getTitleName, requestVo.getTitleName())
                .like(StringUtils.isNotBlank(requestVo.getSanctionsName()), Sanctions::getSanctionsName, requestVo.getSanctionsName())
                .like(StringUtils.isNotBlank(requestVo.getSanctionsObj()), Sanctions::getSanctionsObj, requestVo.getSanctionsObj())
                .eq(StringUtils.isNotBlank(requestVo.getSanctionsType()), Sanctions::getSanctionsType, requestVo.getSanctionsType())
                .eq(StringUtils.isNotBlank(requestVo.getStatus()), Sanctions::getStatus, requestVo.getStatus())
                .eq(StringUtils.isNotBlank(requestVo.getExecDept()), Sanctions::getExecDept, requestVo.getExecDept())
                .orderByAsc(Sanctions::getCreateTime);

        //构建分页
        Page<Sanctions> page = sanctionsService.page(
                new Page<>(requestVo.getPageNum(), requestVo.getPageSize()), wrapper);

        return R.ok(page, "获取奖惩信息列表成功");
    }

    /**
     * 根据id查询信息
     * @param sanctionsId
     * @return
     */
    @GetMapping(value = "/{sanctionsId}")
    @ApiOperation(value = "根据id查询信息")
    public R<Sanctions> select(@PathVariable Long sanctionsId) {
        Sanctions violateById = sanctionsService.getById(sanctionsId);
        return R.ok(violateById, "查看详情信息成功");
    }

    /**
     * 删除奖惩信息
     * @param ids
     * @return
     */
    @Log(title = "删除奖惩信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除奖惩信息(逻辑删除)")
    public R<String> remove(@PathVariable List<Long> ids) {
        sanctionsService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

    /**
     * 新增奖惩信息
     * @param sanctions
     * @return
     */
    @Log(title = "新增奖惩信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增奖惩信息")
    public R<Sanctions> add(@Validated(Insert.class) @RequestBody Sanctions sanctions) {
        sanctions.setCreateBy(SecurityUtils.getUsername());
        sanctionsService.save(sanctions);
        return R.ok(sanctions,"新增成功");
    }

    /**
     * 更新奖惩信息
     * @param sanctions
     * @return
     */
    @Log(title = "更新奖惩信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "更新奖惩信息")
    public R<Sanctions> update(@Validated(Update.class) @RequestBody Sanctions sanctions) {
        sanctions.setUpdateBy(SecurityUtils.getUsername());
        sanctionsService.updateById(sanctions);
        return R.ok(sanctions,"保存成功");
    }

}
